package com.java.controller;


import com.java.bean.BootStrapTableExpress;
import com.java.bean.Express;
import com.java.bean.Message;
import com.java.bean.User;
import com.java.mvc.ResponseBody;
import com.java.service.ExpressService;
import com.java.util.DateFormatUtil;
import com.java.util.JSONUtil;
import com.java.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class WxUserController {

    @ResponseBody("/wx/findExpressByUserPhone.do")
    public String findExpressByUserPhone(HttpServletRequest request, HttpServletResponse response) {
        Message msg = new Message();

        User wxUser = UserUtil.getWxUser(request.getSession());
        String phone = wxUser.getPhone();
        List<Express> list = ExpressService.findByUserPhone(phone);

        if(list.size() > 0) {
            List<BootStrapTableExpress> list2 = new ArrayList<>();
            for (Express express : list) {
                String inTime = DateFormatUtil.format(express.getInTime());
                String outTime = express.getOutTime() == null? "未出库":DateFormatUtil.format(express.getOutTime());
                String status = express.getStatus() == 0 ? "待取件":"已取件";
                String code = express.getCode() == null ? "已取件" : express.getCode();
                BootStrapTableExpress e2 = new BootStrapTableExpress(express.getId(),express.getNumber(),express.getUsername(),express.getUserPhone(),express.getCompany(),code,inTime,outTime,status,express.getSysPhone());
                list2.add(e2);
            }
            Stream<BootStrapTableExpress> status0Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("待取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1Time - o2Time);
            });
            Stream<BootStrapTableExpress> status1Express = list2.stream().filter(express -> {
                if (express.getStatus().equals("已取件")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                long o1Time = DateFormatUtil.toTime(o1.getInTime());
                long o2Time = DateFormatUtil.toTime(o2.getInTime());
                return (int) (o1Time - o2Time);
            });

            Map<String, Object[]> data = new HashMap<>();
            data.put("status0", status0Express.toArray());
            data.put("status1", status1Express.toArray());

            msg.setStatus(0);
            msg.setData(data);
        } else {
            msg.setStatus(-1);
            msg.setResult("当前用户没有快递信息");
        }
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/userExpressList.do")
    public String expressList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("11111111111111111111111start");
        Message msg = new Message();
        String userPhone = request.getParameter("userPhone");
        List<Express> li = ExpressService.findByUserPhoneAndStatus(userPhone, 0);

        if(li.size() == 0) {
            msg.setStatus(-1);
            msg.setResult("未查询到快递");
        } else {
            List<BootStrapTableExpress> list2 = new ArrayList<>();
            for (Express express : li) {
                String inTime = DateFormatUtil.format(express.getInTime());
                String outTime = express.getOutTime() == null? "未出库":DateFormatUtil.format(express.getOutTime());
                String status = express.getStatus() == 0 ? "待取件":"已取件";
                String code = express.getCode() == null ? "已取件" : express.getCode();
                BootStrapTableExpress e2 = new BootStrapTableExpress(express.getId(),express.getNumber(),express.getUsername(),express.getUserPhone(),express.getCompany(),code,inTime,outTime,status,express.getSysPhone());
                list2.add(e2);
            }
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(list2);
        }
        return JSONUtil.toJSON(msg);
    }
}
